<?php namespace Phpcmf\Controllers\Admin;

// 商品折扣
class Promotion extends \Phpcmf\Table
{

    public function __construct(...$params)
    {
        parent::__construct(...$params);
        $this->name = dr_lang('商品折扣');
        $this->is_data = 0;
        $this->tpl_prefix = 'promotion_';
        $field = [
            'title' => array(
                'ismain' => 1,
                'name' => dr_lang('主题'),
                'fieldname' => 'title',
                'fieldtype' => 'Text',
            ),
            'description' => array(
                'ismain' => 1,
                'name' => dr_lang('描述'),
                'fieldname' => 'description',
                'fieldtype' => 'Text',
            ),
        ];
        $this->_init([
            'table' => SITE_ID.'_store_goods_promotion',
            'field' => $field,
            'date_field' => 'inputtime',
            'show_field' => 'title',
            'order_by' => 'inputtime desc',
        ]);
        $this->ptype = [
            '1' => '折扣',
            '2' => '减价',
            '3' => '固定价',
            '4' => '返现金',
            '5' => '返金币',
        ];
        \Phpcmf\Service::V()->assign([
            'menu' => \Phpcmf\Service::M('Auth')->_admin_menu(
                [
                    $this->name => [APP_DIR.'/'.\Phpcmf\Service::L('Router')->class.'/index', 'fa fa-gift'],
                    '添加' => [APP_DIR.'/'.\Phpcmf\Service::L('Router')->class.'/add', 'fa fa-plus'],
                    '修改' => ['hide:'.APP_DIR.'/'.\Phpcmf\Service::L('Router')->class.'/edit', 'fa fa-edit'],
                    'help' => [244],
                ]
            ),
            'field' => $field,
            'ptype' => $this->ptype,
        ]);
    }

    // 后台查看列表
    public function index() {
        list($tpl) = $this->_List();
        \Phpcmf\Service::V()->display($tpl);
    }

    // 后台添加内容
    public function add() {

        list($tpl) = $this->_Post(0);

        \Phpcmf\Service::V()->assign([
            'xgs' => 0,
            'type' => 0,
            'status' => 1,
            'stime' => SYS_TIME,
            'etime' => SYS_TIME + 3600 * 24,
            'groups' => [],
        ]);
        \Phpcmf\Service::V()->display($tpl);
    }

    // 后台修改内容
    public function edit() {
        list($tpl) = $this->_Post(intval(\Phpcmf\Service::L('Input')->get('id')));
        \Phpcmf\Service::V()->display($tpl);
    }


    // 后台删除表单内容
    public function del() {
        $this->_Del(
            \Phpcmf\Service::L('Input')->get_post_ids(),
            null,
            function ($rows) {
                foreach ($rows as $t) {
                    \Phpcmf\Service::M()->table(SITE_ID.'_store_goods_promotion_item')->where('pid', $t['id'])->delete();
                }
            },
           \Phpcmf\Service::M()->dbprefix($this->init['table'])
        );
    }

    // 隐藏或者启用
    public function hidden_edit() {

        $id = (int)\Phpcmf\Service::L('Input')->get('id');
        $row =\Phpcmf\Service::M()->table($this->init['table'])->get($id);
        !$row && $this->_json(0, dr_lang('数据不存在'));

        $v = $row['status'] ? 0 : 1;
        \Phpcmf\Service::M()->table($this->init['table'])->update($id, ['status' => $v]);

        exit($this->_json(1, dr_lang($v ? '已开启' : '已禁用'), ['value' => $v]));
    }

    /**
     * 获取内容
     * $id      内容id,新增为0
     * */
    protected function _Data($id = 0) {

        $row = parent::_Data($id);
        if ($row) {
            $data = \Phpcmf\Service::M()->table(SITE_ID.'_store')->where('`id` IN (select cid from `'.\Phpcmf\Service::M()->dbprefix(SITE_ID.'_store_goods_promotion_item').'` where pid='.$id.')')->getAll();
            $row['goods_html'] = dr_store_goods_html($data);
            $row['groups'] = dr_string2array($row['member']);
        }

        return $row;
    }

    // 格式化保存数据
    protected function _Format_Data($sid, $data, $old) {

        $data = \Phpcmf\Service::L('Input')->post('data');
        $value = \Phpcmf\Service::L('Input')->post('value');
        $goods = \Phpcmf\Service::L('Input')->post('goods');
        !$goods && $this->_json(0, dr_lang('没有选择商品'));
        !$data['title'] && $this->_json(0, dr_lang('活动主题必须填写'));
        !$data['type'] && $this->_json(0, dr_lang('促销类型必须选择'));

        $save = [
            'type' => (int)$data['type'],
            'title' => $data['title'],
            'xgs' => (int)$data['xgs'],
            'description' => (string)$data['description'],
            'value' => (int)$value[$data['type']],
            'member' => dr_array2string($data['member']),
            'stime' => (int)strtotime($data['stime']),
            'etime' => (int)strtotime($data['etime']),
            'status' => (int)$data['status'],
        ];
        $save['stime'] > $save['etime'] && $this->_json(0, dr_lang('开始时间不能超过结束时间'));
        if (!$sid) {
            $save['nums'] = 0;
            $save['buy_num'] = 0;
            $save['inputtime'] = SYS_TIME;
        }

        return $save;
    }

    // 保存
    protected function _Save($id = 0, $data = [], $old = [], $func = null, $func2 = null) {
        return parent::_Save($id, $data, $old, null, function($id, $data, $old){
            $goods = array_unique(\Phpcmf\Service::L('Input')->post('goods'));
            \Phpcmf\Service::M()->table(SITE_ID.'_store_goods_promotion_item')->where('pid', $id)->delete();
            $c = 0;
            foreach ($goods as $t) {
                $c++;
                \Phpcmf\Service::M()->table(SITE_ID.'_store_goods_promotion_item')->insert([
                    'pid' => $id,
                    'cid' => $t,
                ]);
            }
            \Phpcmf\Service::M()->table(SITE_ID.'_store_goods_promotion')->update($id, [
                'nums' => $c,
            ]);
            return dr_return_data(1, 'ok');
        });
    }


}
